package scuba.DiveBuddy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Period;
import scuba.DiveBuddy.Adapters.HistoryEntriesArrayAdapter;
import scuba.DiveBuddy.Class.DiverProfile;
import scuba.DiveBuddy.Class.LogEntries;
import scuba.DiveBuddy.Class.LogEntry;
import scuba.DiveBuddy.Class.LogPageSanitiser;
import scuba.DiveBuddy.Core.Toolbelt;
import scuba.DiveBuddy.data.LogEntrySource;

import java.util.ArrayList;

/**
 * @author Alexander Anderson
 * @since 24/11/13
 * @version 1
 */
public class HomepageActivity extends Activity {

    //"Constructor"
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        setTitle(R.string.activity_header_viewinghistory);

        this.updateInterface();
    }

    //<editor-fold desc="----> OnResume()">
    @Override
    protected void onResume() {
        super.onResume();
        this.updateInterface();
    }
    //</editor-fold>

    //<editor-fold desc="----> Options Menu">
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId())
        {
            //case R.id.action_createmix:
                //invokeCreateMix();
               // break;

            case R.id.action_cleardives:
                LogEntrySource.Cleanse();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.homepage_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
    //</editor-fold>

    private void updateInterface() {

        DiverProfile curr = DiverProfile.getActiveProfile();
        //Set Texts on the Overview
        TextView t = (TextView)findViewById(R.id.profileDisplay);
        //t.setText(curr.getName());

        t = (TextView)findViewById(R.id.totalDiveDisplay);
        t.setText(LogEntries.getInstance().getCumulativeDives() + " Dives");

        t = (TextView)findViewById(R.id.totalDiveTimeDisplay);
        Period cumulativeTime = LogEntries.getInstance().getCumulativeTime();
        Hours hrs = cumulativeTime.toStandardHours();
        Minutes min = cumulativeTime.toStandardMinutes();

        t.setText(hrs.getHours() + "hrs" + min.getMinutes() % 60 + "m Logged");
    }

    //<editor-fold desc="----> invokes">
    public void invokeCreateNewLogEntry(View v)
    {
        //Create new
        LogEntries.getInstance().CreateNew();
        LogPageSanitiser.Reset();

        //Start Activity
        Intent i = new Intent(this, LogEntryEditorActivity.class);
        startActivity(i);
    }
    public void invokeViewHistory(View v)
    {
        Intent i = new Intent(this, HistoryActivity.class);
        startActivity(i);
    }
    public void invokeEditProfile(View v)
    {
        Intent i = new Intent(this, EditProfileActivity.class);
        startActivity(i);
    }
    public void invokeViewMetrics(View v)
    {
        Intent i = new Intent(this, MetricsActivity.class);
        startActivity(i);
    }
    public void invokeCreatePlan(View v)
    {
        //Intent i = new Intent(this, MetricsActivity.class);
        //startActivity(i);
        Toolbelt.Toolkit.showToast(this, "Functionality not supported.");
    }

    //</editor-fold>
}